/*
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 * 
 * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 * 
 * 1. Definitions.
 * 
 * "License" shall mean the terms and conditions for use, reproduction, and
 * distribution as defined by Sections 1 through 9 of this document.
 * 
 * "Licensor" shall mean the copyright owner or entity authorized by the copyright
 * owner that is granting the License.
 * 
 * "Legal Entity" shall mean the union of the acting entity and all other entities
 * that control, are controlled by, or are under common control with that entity.
 * For the purposes of this definition, "control" means (i) the power, direct or
 * indirect, to cause the direction or management of such entity, whether by
 * contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
 * outstanding shares, or (iii) beneficial ownership of such entity.
 * 
 * "You" (or "Your") shall mean an individual or Legal Entity exercising
 * permissions granted by this License.
 * 
 * "Source" form shall mean the preferred form for making modifications, including
 * but not limited to software source code, documentation source, and configuration
 * files.
 * 
 * "Object" form shall mean any form resulting from mechanical transformation or
 * translation of a Source form, including but not limited to compiled object code,
 * generated documentation, and conversions to other media types.
 * 
 * "Work" shall mean the work of authorship, whether in Source or Object form, made
 * available under the License, as indicated by a copyright notice that is included
 * in or attached to the work (an example is provided in the Appendix below).
 * 
 * "Derivative Works" shall mean any work, whether in Source or Object form, that
 * is based on (or derived from) the Work and for which the editorial revisions,
 * annotations, elaborations, or other modifications represent, as a whole, an
 * original work of authorship. For the purposes of this License, Derivative Works
 * shall not include works that remain separable from, or merely link (or bind by
 * name) to the interfaces of, the Work and Derivative Works thereof.
 * 
 * "Contribution" shall mean any work of authorship, including the original version
 * of the Work and any modifications or additions to that Work or Derivative Works
 * thereof, that is intentionally submitted to Licensor for inclusion in the Work
 * by the copyright owner or by an individual or Legal Entity authorized to submit
 * on behalf of the copyright owner. For the purposes of this definition,
 * "submitted" means any form of electronic, verbal, or written communication sent
 * to the Licensor or its representatives, including but not limited to
 * communication on electronic mailing lists, source code control systems, and
 * issue tracking systems that are managed by, or on behalf of, the Licensor for
 * the purpose of discussing and improving the Work, but excluding communication
 * that is conspicuously marked or otherwise designated in writing by the copyright
 * owner as "Not a Contribution."
 * 
 * "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
 * of whom a Contribution has been received by Licensor and subsequently
 * incorporated within the Work.
 * 
 * 2. Grant of Copyright License.
 * 
 * Subject to the terms and conditions of this License, each Contributor hereby
 * grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
 * irrevocable copyright license to reproduce, prepare Derivative Works of,
 * publicly display, publicly perform, sublicense, and distribute the Work and such
 * Derivative Works in Source or Object form.
 * 
 * 3. Grant of Patent License.
 * 
 * Subject to the terms and conditions of this License, each Contributor hereby
 * grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
 * irrevocable (except as stated in this section) patent license to make, have
 * made, use, offer to sell, sell, import, and otherwise transfer the Work, where
 * such license applies only to those patent claims licensable by such Contributor
 * that are necessarily infringed by their Contribution(s) alone or by combination
 * of their Contribution(s) with the Work to which such Contribution(s) was
 * submitted. If You institute patent litigation against any entity (including a
 * cross-claim or counterclaim in a lawsuit) alleging that the Work or a
 * Contribution incorporated within the Work constitutes direct or contributory
 * patent infringement, then any patent licenses granted to You under this License
 * for that Work shall terminate as of the date such litigation is filed.
 * 
 * 4. Redistribution.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof
 * in any medium, with or without modifications, and in Source or Object form,
 * provided that You meet the following conditions:
 * 
 * You must give any other recipients of the Work or Derivative Works a copy of
 * this License; and
 * You must cause any modified files to carry prominent notices stating that You
 * changed the files; and
 * You must retain, in the Source form of any Derivative Works that You distribute,
 * all copyright, patent, trademark, and attribution notices from the Source form
 * of the Work, excluding those notices that do not pertain to any part of the
 * Derivative Works; and
 * If the Work includes a "NOTICE" text file as part of its distribution, then any
 * Derivative Works that You distribute must include a readable copy of the
 * attribution notices contained within such NOTICE file, excluding those notices
 * that do not pertain to any part of the Derivative Works, in at least one of the
 * following places: within a NOTICE text file distributed as part of the
 * Derivative Works; within the Source form or documentation, if provided along
 * with the Derivative Works; or, within a display generated by the Derivative
 * Works, if and wherever such third-party notices normally appear. The contents of
 * the NOTICE file are for informational purposes only and do not modify the
 * License. You may add Your own attribution notices within Derivative Works that
 * You distribute, alongside or as an addendum to the NOTICE text from the Work,
 * provided that such additional attribution notices cannot be construed as
 * modifying the License.
 * You may add Your own copyright statement to Your modifications and may provide
 * additional or different license terms and conditions for use, reproduction, or
 * distribution of Your modifications, or for any such Derivative Works as a whole,
 * provided Your use, reproduction, and distribution of the Work otherwise complies
 * with the conditions stated in this License.
 * 
 * 5. Submission of Contributions.
 * 
 * Unless You explicitly state otherwise, any Contribution intentionally submitted
 * for inclusion in the Work by You to the Licensor shall be under the terms and
 * conditions of this License, without any additional terms or conditions.
 * Notwithstanding the above, nothing herein shall supersede or modify the terms of
 * any separate license agreement you may have executed with Licensor regarding
 * such Contributions.
 * 
 * 6. Trademarks.
 * 
 * This License does not grant permission to use the trade names, trademarks,
 * service marks, or product names of the Licensor, except as required for
 * reasonable and customary use in describing the origin of the Work and
 * reproducing the content of the NOTICE file.
 * 
 * 7. Disclaimer of Warranty.
 * 
 * Unless required by applicable law or agreed to in writing, Licensor provides the
 * Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
 * including, without limitation, any warranties or conditions of TITLE,
 * NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
 * solely responsible for determining the appropriateness of using or
 * redistributing the Work and assume any risks associated with Your exercise of
 * permissions under this License.
 * 
 * 8. Limitation of Liability.
 * 
 * In no event and under no legal theory, whether in tort (including negligence),
 * contract, or otherwise, unless required by applicable law (such as deliberate
 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
 * liable to You for damages, including any direct, indirect, special, incidental,
 * or consequential damages of any character arising as a result of this License or
 * out of the use or inability to use the Work (including but not limited to
 * damages for loss of goodwill, work stoppage, computer failure or malfunction, or
 * any and all other commercial damages or losses), even if such Contributor has
 * been advised of the possibility of such damages.
 * 
 * 9. Accepting Warranty or Additional Liability.
 * 
 * While redistributing the Work or Derivative Works thereof, You may choose to
 * offer, and charge a fee for, acceptance of support, warranty, indemnity, or
 * other liability obligations and/or rights consistent with this License. However,
 * in accepting such obligations, You may act only on Your own behalf and on Your
 * sole responsibility, not on behalf of any other Contributor, and only if You
 * agree to indemnify, defend, and hold each Contributor harmless for any liability
 * incurred by, or claims asserted against, such Contributor by reason of your
 * accepting any such warranty or additional liability.
 * 
 * END OF TERMS AND CONDITIONS
 * 
 * APPENDIX: How to apply the Apache License to your work
 * 
 * To apply the Apache License to your work, attach the following boilerplate
 * notice, with the fields enclosed by brackets "{}" replaced with your own
 * identifying information. (Don't include the brackets!) The text should be
 * enclosed in the appropriate comment syntax for the file format. We also
 * recommend that a file or class name and description of purpose be included on
 * the same "printed page" as the copyright notice for easier identification within
 * third-party archives.
 * 
 *    Copyright 2016 guerlab(http://www.guerlab.net)
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package net.guerlab.wechat.exception;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信服务错误异常类，对所有的异常返回进行了处理分类
 * 
 * @author guer
 *
 */
public class WeChatException extends Exception {

	private static final long serialVersionUID = 1L;

	/**
	 * 错误代码
	 */
	private int errcode;

	/**
	 * 错误信息
	 */
	private String errmsg;
	
	/**
	 * 提示信息列表
	 */
	private static final transient Map<Integer, String> MESSAGES = new HashMap<>(); 
	
	/**
	 * 默认提示信息
	 */
	private static final String DEFAULT_MESSAGE = "未知错误";

	static {
		MESSAGES.put(-1, "系统繁忙，此时请开发者稍候再试");
		MESSAGES.put(0, "请求成功");
		MESSAGES.put(40001, "获取access_token时AppSecret错误，或者access_token无效。请开发者认真比对AppSecret的正确性，或查看是否正在为恰当的公众号调用接口");
		MESSAGES.put(40002, "不合法的凭证类型");
		MESSAGES.put(40003, "不合法的OpenID，请开发者确认OpenID（该用户）是否已关注公众号，或是否是其他公众号的OpenID");
		MESSAGES.put(40004, "不合法的媒体文件类型");
		MESSAGES.put(40005, "不合法的文件类型");
		MESSAGES.put(40006, "不合法的文件大小");
		MESSAGES.put(40007, "不合法的媒体文件id");
		MESSAGES.put(40008, "不合法的消息类型");
		MESSAGES.put(40009, "不合法的图片文件大小");
		MESSAGES.put(40010, "不合法的语音文件大小");
		MESSAGES.put(40011, "不合法的视频文件大小");
		MESSAGES.put(40012, "不合法的缩略图文件大小");
		MESSAGES.put(40013, "不合法的AppID，请开发者检查AppID的正确性，避免异常字符，注意大小写");
		MESSAGES.put(40014, "不合法的access_token，请开发者认真比对access_token的有效性（如是否过期），或查看是否正在为恰当的公众号调用接口");
		MESSAGES.put(40015, "不合法的菜单类型");
		MESSAGES.put(40016, "不合法的按钮个数");
		MESSAGES.put(40017, "不合法的按钮个数");
		MESSAGES.put(40018, "不合法的按钮名字长度");
		MESSAGES.put(40019, "不合法的按钮KEY长度");
		MESSAGES.put(40020, "不合法的按钮URL长度");
		MESSAGES.put(40021, "不合法的菜单版本号");
		MESSAGES.put(40022, "不合法的子菜单级数");
		MESSAGES.put(40023, "不合法的子菜单按钮个数");
		MESSAGES.put(40024, "不合法的子菜单按钮类型");
		MESSAGES.put(40025, "不合法的子菜单按钮名字长度");
		MESSAGES.put(40026, "不合法的子菜单按钮KEY长度");
		MESSAGES.put(40027, "不合法的子菜单按钮URL长度");
		MESSAGES.put(40028, "不合法的自定义菜单使用用户");
		MESSAGES.put(40029, "不合法的oauth_code");
		MESSAGES.put(40030, "不合法的refresh_token");
		MESSAGES.put(40031, "不合法的openid列表");
		MESSAGES.put(40032, "不合法的openid列表长度");
		MESSAGES.put(40033, "不合法的请求字符，不能包含\\uxxxx格式的字符");
		MESSAGES.put(40035, "不合法的参数");
		MESSAGES.put(40038, "不合法的请求格式");
		MESSAGES.put(40039, "不合法的URL长度");
		MESSAGES.put(40050, "不合法的分组id");
		MESSAGES.put(40051, "分组名字不合法");
		MESSAGES.put(40117, "分组名字不合法");
		MESSAGES.put(40118, "media_id大小不合法");
		MESSAGES.put(40119, "button类型错误");
		MESSAGES.put(40120, "button类型错误");
		MESSAGES.put(40121, "不合法的media_id类型");
		MESSAGES.put(40132, "微信号不合法");
		MESSAGES.put(40137, "不支持的图片格式");
		MESSAGES.put(41001, "缺少access_token参数");
		MESSAGES.put(41002, "缺少appid参数");
		MESSAGES.put(41003, "缺少refresh_token参数");
		MESSAGES.put(41004, "缺少secret参数");
		MESSAGES.put(41005, "缺少多媒体文件数据");
		MESSAGES.put(41006, "缺少media_id参数");
		MESSAGES.put(41007, "缺少子菜单数据");
		MESSAGES.put(41008, "缺少oauth code");
		MESSAGES.put(41009, "缺少openid");
		MESSAGES.put(42001, "access_token超时，请检查access_token的有效期，请参考基础支持-获取access_token中，对access_token的详细机制说明");
		MESSAGES.put(42002, "refresh_token超时");
		MESSAGES.put(42003, "oauth_code超时");
		MESSAGES.put(43001, "需要GET请求");
		MESSAGES.put(43002, "需要POST请求");
		MESSAGES.put(43003, "需要HTTPS请求");
		MESSAGES.put(43004, "需要接收者关注");
		MESSAGES.put(43005, "需要好友关系");
		MESSAGES.put(44001, "多媒体文件为空");
		MESSAGES.put(44002, "POST的数据包为空");
		MESSAGES.put(44003, "图文消息内容为空");
		MESSAGES.put(44004, "文本消息内容为空");
		MESSAGES.put(45001, "多媒体文件大小超过限制");
		MESSAGES.put(45002, "消息内容超过限制");
		MESSAGES.put(45003, "标题字段超过限制");
		MESSAGES.put(45004, "描述字段超过限制");
		MESSAGES.put(45005, "链接字段超过限制");
		MESSAGES.put(45006, "图片链接字段超过限制");
		MESSAGES.put(45007, "语音播放时间超过限制");
		MESSAGES.put(45008, "图文消息超过限制");
		MESSAGES.put(45009, "接口调用超过限制");
		MESSAGES.put(45010, "创建菜单个数超过限制");
		MESSAGES.put(45015, "回复时间超过限制");
		MESSAGES.put(45016, "系统分组，不允许修改");
		MESSAGES.put(45017, "分组名字过长");
		MESSAGES.put(45018, "分组数量超过上限");
        MESSAGES.put(45056, "创建的标签数过多，请注意不能超过100个");
        MESSAGES.put(45058, "不能修改0/1/2这三个系统默认保留的标签");
        MESSAGES.put(45059, "有粉丝身上的标签数已经超过限制");
        MESSAGES.put(45057, "该标签下粉丝数超过10w，不允许直接删除");
        MESSAGES.put(45157, "标签名非法，请注意不能和其他标签重名");
        MESSAGES.put(45158, "标签名长度超过30个字节");
        MESSAGES.put(45159, "非法的tag_id");
		MESSAGES.put(46001, "不存在媒体数据");
		MESSAGES.put(46002, "不存在的菜单版本");
		MESSAGES.put(46003, "不存在的菜单数据");
		MESSAGES.put(46004, "不存在的用户");
		MESSAGES.put(47001, "解析JSON/XML内容错误");
		MESSAGES.put(48001, "api功能未授权，请确认公众号已获得该接口，可以在公众平台官网-开发者中心页中查看接口权限");
        MESSAGES.put(49003, "传入的openid不属于此AppID");
		MESSAGES.put(50001, "用户未授权该api");
		MESSAGES.put(50002, "用户受限，可能是违规后接口被封禁");
		MESSAGES.put(61451, "参数错误(invalid parameter)");
		MESSAGES.put(61452, "无效客服账号(invalid kf_account)");
		MESSAGES.put(61453, "客服帐号已存在(kf_account exsited)");
		MESSAGES.put(61454, "客服帐号名长度超过限制(仅允许10个英文字符，不包括@及@后的公众号的微信号)(invalid kf_acount length)");
		MESSAGES.put(61455, "客服帐号名包含非法字符(仅允许英文+数字)(illegal character in kf_account)");
		MESSAGES.put(61456, "客服帐号个数超过限制(10个客服账号)(kf_account count exceeded)");
		MESSAGES.put(61457, "无效头像文件类型(invalid file type)");
		MESSAGES.put(61450, "系统错误(system error)");
		MESSAGES.put(61500, "日期格式错误");
		MESSAGES.put(61501, "日期范围错误");
		MESSAGES.put(9001001, "POST数据参数不合法");
		MESSAGES.put(9001002, "远端服务不可用");
		MESSAGES.put(9001003, "Ticket不合法");
		MESSAGES.put(9001004, "获取摇周边用户信息失败");
		MESSAGES.put(9001005, "获取商户信息失败");
		MESSAGES.put(9001006, "获取OpenID失败");
		MESSAGES.put(9001007, "上传文件缺失");
		MESSAGES.put(9001008, "上传素材的文件类型不合法");
		MESSAGES.put(9001009, "上传素材的文件尺寸不合法");
		MESSAGES.put(9001010, "上传失败");
		MESSAGES.put(9001020, "帐号不合法");
		MESSAGES.put(9001021, "已有设备激活率低于50%，不能新增设备");
		MESSAGES.put(9001022, "设备申请数不合法，必须为大于0的数字");
		MESSAGES.put(9001023, "已存在审核中的设备ID申请");
		MESSAGES.put(9001024, "一次查询设备ID数量不能超过50");
		MESSAGES.put(9001025, "设备ID不合法");
		MESSAGES.put(9001026, "页面ID不合法");
		MESSAGES.put(9001027, "页面参数不合法");
		MESSAGES.put(9001028, "一次删除页面ID数量不能超过10");
		MESSAGES.put(9001029, "页面已应用在设备中，请先解除应用关系再删除");
		MESSAGES.put(9001030, "一次查询页面ID数量不能超过50");
		MESSAGES.put(9001031, "时间区间不合法");
		MESSAGES.put(9001032, "保存设备与页面的绑定关系参数错误");
		MESSAGES.put(9001033, "门店ID不合法");
		MESSAGES.put(9001034, "设备备注信息过长");
		MESSAGES.put(9001035, "设备申请参数不合法");
		MESSAGES.put(9001036, "查询起始值begin不合法");
		MESSAGES.put(null, DEFAULT_MESSAGE);
	}
	
	public WeChatException(int errcode, String errmsg) {
		super(errmsg);
		this.errcode = errcode;
		this.errmsg = errmsg;
	}

	/**
	 * 获取错误代码
	 * 
	 * @return 错误代码
	 */
	public int getErrcode() {
		return errcode;
	}

	/**
	 * 获取错误信息
	 * 
	 * @return 错误信息
	 */
	public String getErrmsg() {
		return errmsg;
	}

	@Override
	public String toString() {
		return "RequestException [errcode : " + errcode + ", errmsg : "
		        + errmsg + "] " + getInfo();
	}

	/**
	 * 获取详细错误信息
	 * 
	 * @return 详细错误信息
	 */
	public final String getInfo() {
		String message = MESSAGES.get(errcode);
		
		return message == null ? DEFAULT_MESSAGE : message;
	}

}
